import re

from django import http
from django.shortcuts import render, redirect
from django_redis import get_redis_connection
from django.contrib.auth import authenticate, login
# Create your views here.

from django.views import View

from users.models import User

# 注册页面视图
class RegisterView(View):
    def get(self,request):
        return render(request,'register.html')

    def post(self,request):
        # 请求路径
        # 请求方式
        # 请求参数
        # 响应体

        user_name = request.POST.get('user_name')
        pwd = request.POST.get('pwd')
        cpwd = request.POST.get('cpwd')
        phone = request.POST.get('phone')
        msg_code = request.POST.get('msg_code')
        allow = request.POST.get('allow')

        # 为空检验
        if not all([user_name,pwd,cpwd,phone,msg_code,allow]):
            return http.HttpResponseForbidden('参数不全')
        # 密码校验
        if pwd != cpwd:
            return http.HttpResponseForbidden('两次密码错误')
        # 手机号正确
        if not re.match('^1[3-9]\d{9}$',phone):
            return http.HttpResponseForbidden('手机号错误')
        # 短信验证码校验
        redis_store = get_redis_connection('code')
        sms_code = redis_store.get('sms_code_%s'%phone).decode()
        if sms_code != msg_code:
            return http.HttpResponseForbidden('短信错误')

        # 协议同意
        if allow != 'on':
            return http.HttpResponseForbidden('协议未同意')
        # 数据入库
        # user = User.objects.create(username=user_name,password=pwd,mobile=phone)
        # 这个create_user 可以对密码加密
        user = User.objects.create_user(username=user_name,password=pwd,mobile=phone)

        # 返回响应
        return redirect('http://www.taobao.com')

# 检查用户名是否重复视图
class CheckUserameView(View):
    def get(self,request,username):
        # 根据用户名查询用户数量
        count = User.objects.filter(username=username).count()

        # 返回响应
        return http.JsonResponse({'count':count,'code': 0})

# 检查手机号是否重复视图
class CheckPhoneView(View):
    def get(self,request,mobile):
        count = User.objects.filter(mobile=mobile).count()

        return http.JsonResponse({'count':count,'code': 0})

# 登陆shitu
class LoginView(View):
    def get(self,request):
        return render(request,'login.html')
    def post(self,request):
        # 请求参数
        username = request.POST.get('username')
        pwd = request.POST.get('pwd')
        remembered = request.POST.get('remembered')

        # 为空校验
        if not all([username,pwd]):
            return http.HttpResponseForbidden('参数不全')

        # 用户格式名校验
        if not re.match(r'^[a-zA-Z0-9_-]{5,20}$',username):
            return http.HttpResponseForbidden('用户名格式错误')
        # 密码格式校验
        if not re.match('^[0-9A-Za-z]{8,20}$',pwd):
            return http.HttpResponseForbidden('密码名格式错误')
        # 密码校验
        user = authenticate(request, username=username, password=pwd)
        if not user:
            return http.HttpResponseForbidden('账号或者密码不正确')

        # 数据入库状态保持
        login(request, user)  #就是将用户的信息存储在SESSION中
        if remembered == 'on':
            request.session.set_expiry(3600*24*2)  # 设置用户的有效期 2天
        else:
            request.session.set_expiry(0) # 一次浏览器会话结束

        # 返回响应
        return redirect('/')